﻿namespace ManagerClinic.DanhMuc
{
    using System.Xml;
    using System.Runtime.InteropServices;
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Text;
    using System.Windows.Forms;
    using DevExpress.XtraEditors;
    using ManagerClinic.Core.Common;
    using ManagerClinic.Core.Business;
    using ManagerClinic.Modules;
    using ManagerClinic.Ultilities.Ultility;

    public partial class frmCapNhatDmGioiTinh : DevExpress.XtraEditors.XtraForm
    {
        public event EventHandler dmGioiTinhEvent;
        int State = 0;

        ThietLapMaBusiness thietLapMaBusiness = new ThietLapMaBusiness();
        DmGioiTinhBusiness dmGioiTinhBusiness = new DmGioiTinhBusiness();

        DmGioiTinhEntity dmGioiTinhEntity = null;

        public frmCapNhatDmGioiTinh( int state)
        {
            InitializeComponent();
            State = state;
        }

        public frmCapNhatDmGioiTinh(int state, DmGioiTinhEntity obj)
        {
            InitializeComponent();

            State = state;
            dmGioiTinhEntity = obj;

            txtMaGioiTinh.Text = dmGioiTinhEntity.MaGioiTinh;
            txtTenGioiTinh.Text = dmGioiTinhEntity.TenGioiTinh;
            txtMoTa.Text = dmGioiTinhEntity.MoTa;
            chkTrangThaiSuDung.Checked = dmGioiTinhEntity.TrangThaiSuDung;
            txtTenGioiTinh.Focus();
        }

        private void frmcapnhatDMgioitinh_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                SendKeys.SendWait("{Tab}");
            }
        }

        private void btnDong_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnLuu_Click(object sender, EventArgs e)
        {
            if (validateManager1.IsValid() == false) return;
            if (Validation.ValidSpaceTextBoxKey(txtMaGioiTinh, "Mã giới tính") == false) return;
            
            dmGioiTinhEntity = new DmGioiTinhEntity()
            {
                MaGioiTinh = txtMaGioiTinh.Text.Trim(),
                TenGioiTinh = txtTenGioiTinh.Text.Trim(),
                MoTa = txtMoTa.Text.Trim(),
                TrangThaiSuDung = (bool)chkTrangThaiSuDung.Checked
            };

            if (this.State == (int)ConstantManagerClinic.StateInsertOrUpdate.Insert)
            {
                if (dmGioiTinhBusiness.GetDmGioiTinh(txtMaGioiTinh.Text.Trim()) != null)
                    if (Validation.ValidPrimaryKey(txtMaGioiTinh, "Mã giới tính") == false) return;
                dmGioiTinhBusiness.InsertDmGioiTinh(dmGioiTinhEntity);
                Validation.ClearControl(this);
                txtMaGioiTinh.Focus();
            }
            else
            {
                dmGioiTinhBusiness.EditDmGioiTinh(dmGioiTinhEntity);
                MessageBox.Show("Cập nhật thành công!", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }

            if (dmGioiTinhEvent != null)
                dmGioiTinhEvent(sender, e);
        }
    }
}